package com.kanshu.ksgb.fastread.doudou.module.reader.utils;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.view.WindowManager;

import com.kanshu.ksgb.fastread.doudou.common.util.RomUtils;

import java.lang.ref.WeakReference;

/**
 * 屏幕常亮工具类
 */
public class ScreenOnHelper {
    private WeakReference<Activity> mActivity;
    private Handler mHandler;
    private Runnable mRunnable = () -> screenOff();
    public ScreenOnHelper(Activity activity) {
        if (!RomUtils.isSmartisan()) {
            mActivity = new WeakReference<>(activity);
            mHandler = new Handler(Looper.getMainLooper());
            mHandler.postDelayed(mRunnable, SettingManager.getInstance().getScreenOnParam() * 60 * 1000);
        }

    }
    /**
     * 屏幕常亮
     */
    public void screenOn() {
        if (!RomUtils.isSmartisan()) {
            if (mActivity != null && mActivity.get() != null && !mActivity.get().isFinishing()) {
                mActivity.get().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            }
        }
    }
    /**
     * 屏幕熄灭
     */
    public void screenOff() {
        if (!RomUtils.isSmartisan()) {
            if (mActivity != null && mActivity.get() != null && !mActivity.get().isFinishing()) {
                mActivity.get().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            }
        }
    }

    /**
     * 清除动作
     */
    public void clear() {
        if (!RomUtils.isSmartisan()) {
            mHandler.removeCallbacksAndMessages(null);
            screenOff();
        }
    }

    public void setScreenOnMins(int mins) {
        if (!RomUtils.isSmartisan()) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler.postDelayed(mRunnable, mins * 60 * 1000);
        }
    }
}
